package com.yanqu.road.server.cmd.activity.yqhotel;


import com.yanqu.road.entity.activity.yqhotel.result.YqHotelLevelUpStudentResult;
import com.yanqu.road.message.YanQuMessage;

import com.yanqu.road.pb.activity.YqHotelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;

import com.yanqu.road.server.gameplayer.module.activity.yqhotel.YqHotelModule;
import com.yanqu.road.server.manger.activity.yqhotel.YqHotelMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;


@Cmd(code = Protocol.S_YQ_HOTEL_BUILDING_LEVEL_UP, desc = "阳泉酒家(史瑞克)建筑升级")
public class YqHotelBuildingLevelUpCmd extends AbstractCommand {

    private int activityId;
    private int buildingId;
    private int type;//1代表升级坑位的那个技能，2是每个建筑的特殊技能那个

    private YqHotelLevelUpStudentResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YqHotelProto.YqHotelLevelUpBuildingReqMsg reqMsg = YqHotelProto.YqHotelLevelUpBuildingReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        buildingId = reqMsg.getBuildingId();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(YqHotelMgr.getActivityTimeAcId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!YqHotelMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        Object locker = player.getModule(YqHotelModule.class).getLocker();
        synchronized (locker){
            result = player.getModule(YqHotelModule.class).levelUp(buildingId,type);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        YqHotelProto.YqHotelLevelUpBuildingRespMsg.Builder respMsg = YqHotelProto.YqHotelLevelUpBuildingRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            YqHotelProto.YqHotelBuildingTemp.Builder buildingTempMsg = YqHotelProto.YqHotelBuildingTemp.newBuilder();
            buildingTempMsg.setBuildingId(buildingId);
            buildingTempMsg.setBuildingLevel(player.getModule(YqHotelModule.class).getBuildingLevel(buildingId));
            buildingTempMsg.setSkillLevel(player.getModule(YqHotelModule.class).getSkillLevel(buildingId));
            Map<Integer,Long> timeMap = player.getModule(YqHotelModule.class).getBuildingLevelUpTimeMap(buildingId);
            for(long levelUpTime : timeMap.values()){
                buildingTempMsg.addLevelUpTime(levelUpTime);
            }
            respMsg.setBuilding(buildingTempMsg);
        }
        player.sendPacket(Protocol.U_YQ_HOTEL_BUILDING_LEVEL_UP, respMsg);
        //变化的计算一次
        player.getModule(YqHotelModule.class).syncStudent(result.getChangeStudent(),type);
    }
}
